
package com.e885.sample.service;

import java.util.ArrayList;
import java.util.List;

import com.e885.exception.E885UtilException;
import com.e885.hibernate.HibernateSessionFactory;
import com.e885.hibernate.Pagination;
import com.e885.log.E885Logger;
import com.e885.sample.exception.QueryException;
import com.e885.sample.model.bean.PersonInfo;
import com.e885.sample.model.dao.PersonInfoDAO;
import com.e885.sample.view.bean.CountyBean;
import com.e885.sample.view.bean.DistrictBean;
import com.e885.sample.view.bean.PersonInfoBean;
import com.e885.service.CommonService;
import com.e885.util.bean.BeanUtilImprover;

/**
 * @author Eric.Kao(gau168.gau@msa.hinet.net)
 * @version 1.0
 * @company E885 Group
 * @copyright Copyright (c) 2001-2008
 */

public class PersonInfoService extends CommonService
{
	/**
	 * 取得個人資料, by 行政區或鄉鎮市
	 * 
	 * @param districtId
	 * @return
	 * @throws QueryException
	 * @throws E885UtilException
	 */
	@SuppressWarnings( "unchecked" )
	public static List< PersonInfoBean > getPersonInfosByDistrict( String districtId ) throws QueryException, E885UtilException
	{
		E885Logger.serviceLog.info( "取得個人資料 by district:[" + districtId + "]" );

		validNull( districtId, "districtId" );

		try
		{
			return processPersonInfoBeanList( new PersonInfoDAO().findPersonInfoByDistrict( districtId ) );
		}
		catch( RuntimeException e )
		{
			e.printStackTrace();

			E885Logger.serviceLog.error( "無法取得個人資料 by district:[" + districtId + "]" );

			throw new QueryException( "無法取得個人資料 by district:[" + districtId + "]" );
		}
		finally
		{
			HibernateSessionFactory.closeSession();
		}
	}

	/**
	 * 依照性別, 取得個人資料
	 * 
	 * @param sex
	 * @return
	 * @throws QueryException
	 * @throws E885UtilException
	 */
	@SuppressWarnings( "unchecked" )
	public static List< PersonInfoBean > getPersonInfosBySex( String sex ) throws QueryException, E885UtilException
	{
		E885Logger.serviceLog.info( "取得個人資料 by sex:[" + sex + "]" );

		validNull( sex, "sex" );

		try
		{
			return processPersonInfoBeanList( new PersonInfoDAO().findPersonInfoBySex( sex ) );
		}
		catch( RuntimeException e )
		{
			e.printStackTrace();

			E885Logger.serviceLog.error( "無法取得個人資料 by sex:[" + sex + "]" );

			throw new QueryException( "無法取得個人資料 by sex:[" + sex + "]" );
		}
		finally
		{
			HibernateSessionFactory.closeSession();
		}
	}

	@SuppressWarnings( "unchecked" )
	public static Pagination getPersonInfosBySex( String sex, int page ) throws QueryException, E885UtilException
	{
		E885Logger.serviceLog.info( "取得個人資料 by sex:[" + sex + "]" );
		E885Logger.serviceLog.info( "取得個人資料 by sex in page:[" + page + "]" );

		validNull( sex, "sex" );

		try
		{
			Pagination pagination = new PersonInfoDAO().findPersonInfoBySex( sex, page, DEFAULT_PAGE_SIZE );
			pagination.setResults( processPersonInfoBeanList( pagination.getResults() ) );

			return pagination;
		}
		catch( RuntimeException e )
		{
			e.printStackTrace();

			E885Logger.serviceLog.error( "無法取得個人資料 by sex:[" + sex + "]" );

			throw new QueryException( "無法取得個人資料 by sex:[" + sex + "]" );
		}
		finally
		{
			HibernateSessionFactory.closeSession();
		}
	}

	/**
	 * 將 PersonInfo(Persistence) 轉為 PersonInfoBean(view bean)
	 * 
	 * @param list
	 * @return
	 * @throws E885UtilException
	 */
	@SuppressWarnings( "unchecked" )
	private static List processPersonInfoBeanList( List list ) throws E885UtilException
	{
		List< PersonInfoBean > personInfoBeanList = new ArrayList< PersonInfoBean >();

		int size = list.size();
		for( int i = 0; i < size; i++ )
		{
			PersonInfo personInfo = ( PersonInfo )list.get( i );

			PersonInfoBean personInfoBean = new PersonInfoBean();
			DistrictBean districtBean = new DistrictBean();
			CountyBean countyBean = new CountyBean();

			BeanUtilImprover.copyProperties( personInfoBean, personInfo );
			BeanUtilImprover.copyProperties( districtBean, personInfo.getDistrict() );
			BeanUtilImprover.copyProperties( countyBean, personInfo.getDistrict().getCounty() );

			districtBean.setCountyBean( countyBean );
			personInfoBean.setDistrictBean( districtBean );

			personInfoBeanList.add( personInfoBean );
		}

		return personInfoBeanList;
	}
}
